﻿package com.toncen.samepms.net.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.toncen.samepms.common.AbstractDomain;
import com.toncen.samepms.common.IPropertyUniqueble;
import com.toncen.samepms.common.Unique;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.domain.Configuration;
import com.sky.common.date.DateTools;
import com.sky.dataimport.DynamicCellDataCell;
import com.sky.dataimport.DynamicCellDataRow;
import com.sky.dataimport.DynamicRowDataCell;
import com.sky.dataimport.DynamicRowDataRow;
import com.sky.dataimport.StaticDataCell;
import com.sky.dataimport.StaticDataRow;
import com.sky.dataimport.datatype.DateDataType;
import com.sky.dataimport.datatype.IntegerDataType;
import com.sky.dataimport.datatype.StringDataType;
import com.sky.dataimport.formater.UtilDateDataFormater;

/**
 * 描述：信息表NET_INFORMATION的domain文件
 * 
 * @time 2013-5-10
 * @author xyp
 */
@DynamicRowDataRow(title = "动态行信息", sheetIndex = 1, startRowIndex = 2, startCellIndex = 1, inserted = false)
@DynamicCellDataRow(title = "动态列信息", sheetIndex = 1, startRowIndex = 2, startCellIndex = 2, inserted = false)
@StaticDataRow(title = "静态行列信息", sheetIndex = 1)
public class Information extends AbstractDomain implements IPropertyUniqueble {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1365475264067L;

	/**
	 * 主标题
	 */
	@StaticDataCell(rowIndex = 1, cellIndex = 1, title = "主标题")
	@DynamicRowDataCell(cellIndex = 1, title = "主标题", require = false)
	@DynamicCellDataCell(rowIndex = 1, title = "主标题", require = false)
	@StringDataType(max = 200)
	@Unique(column = "MAINTITLE", title = "主标题")
	private String maintitle;

	/**
	 * 副标题
	 */
	@StaticDataCell(rowIndex = 2, cellIndex = 2, title = "副标题")
	@DynamicRowDataCell(cellIndex = 2, title = "副标题", require = false)
	@DynamicCellDataCell(rowIndex = 2, title = "副标题", require = false)
	@StringDataType(max = 200)
	private String subtitle;

	/**
	 * 主题词
	 */
	@StaticDataCell(rowIndex = 3, cellIndex = 3, title = "主题词")
	@DynamicRowDataCell(cellIndex = 3, title = "主题词", require = false)
	@DynamicCellDataCell(rowIndex = 3, title = "主题词", require = false)
	@StringDataType(max = 200)
	private String keyword;

	/**
	 * 作者
	 */
	@StaticDataCell(rowIndex = 4, cellIndex = 4, title = "作者")
	@DynamicRowDataCell(cellIndex = 4, title = "作者", require = false)
	@DynamicCellDataCell(rowIndex = 4, title = "作者", require = false)
	@StringDataType(max = 200)
	private String author;

	/**
	 * 类型
	 */
	@StaticDataCell(rowIndex = 5, cellIndex = 5, title = "类型")
	@DynamicRowDataCell(cellIndex = 5, title = "类型", require = false)
	@DynamicCellDataCell(rowIndex = 5, title = "类型", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int subtype;

	/**
	 * 信息内容
	 */
	@StaticDataCell(rowIndex = 6, cellIndex = 6, title = "信息内容")
	@DynamicRowDataCell(cellIndex = 6, title = "信息内容", require = false)
	@DynamicCellDataCell(rowIndex = 6, title = "信息内容", require = false)
	@StringDataType(max = 16)
	private String content;

	/**
	 * 创建人
	 */
	@StaticDataCell(rowIndex = 7, cellIndex = 7, title = "创建人")
	private com.toncen.samepms.basic.domain.User creator;

	/**
	 * 创建日期
	 */
	@StaticDataCell(rowIndex = 8, cellIndex = 8, title = "创建日期")
	@DynamicRowDataCell(cellIndex = 8, title = "创建日期", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DynamicCellDataCell(rowIndex = 8, title = "创建日期", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DateDataType
	private Date createtime = new Date();

	/**
	 * 发布人
	 */
	@StaticDataCell(rowIndex = 9, cellIndex = 9, title = "发布人")
	private com.toncen.samepms.basic.domain.User publisher;

	/**
	 * 发布日期
	 */
	@StaticDataCell(rowIndex = 10, cellIndex = 10, title = "发布日期")
	@DynamicRowDataCell(cellIndex = 10, title = "发布日期", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DynamicCellDataCell(rowIndex = 10, title = "发布日期", formater = UtilDateDataFormater.class, option = "yyyy-MM-dd", require = false)
	@DateDataType
	private Date publishtime = new Date();

	/**
	 * 是否发布
	 */
	@StaticDataCell(rowIndex = 11, cellIndex = 11, title = "是否发布")
	@DynamicRowDataCell(cellIndex = 11, title = "是否发布", require = false)
	@DynamicCellDataCell(rowIndex = 11, title = "是否发布", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int isPublish = 1;

	/**
	 * 顺序
	 */
	@StaticDataCell(rowIndex = 13, cellIndex = 13, title = "顺序")
	@DynamicRowDataCell(cellIndex = 13, title = "顺序", require = false)
	@DynamicCellDataCell(rowIndex = 13, title = "顺序", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int sequence = 10;

	/**
	 * 浏览次数
	 */
	@StaticDataCell(rowIndex = 15, cellIndex = 15, title = "浏览次数")
	@DynamicRowDataCell(cellIndex = 15, title = "浏览次数", require = false)
	@DynamicCellDataCell(rowIndex = 15, title = "浏览次数", require = false)
	@IntegerDataType(msg = "必须为整数类型")
	private int browsecount;

	private List<ChanelInformation> chanelInformation = new ArrayList<ChanelInformation>();

	public Information() {
		super();
	}

	/**
	 * @return the maintitle
	 */
	public String getMaintitle() {
		return maintitle;
	}

	/**
	 * @param maintitle
	 *            the maintitle to set
	 */
	public void setMaintitle(String maintitle) {
		this.maintitle = maintitle;
	}

	/**
	 * @return the subtitle
	 */
	public String getSubtitle() {
		return subtitle;
	}

	/**
	 * @param subtitle
	 *            the subtitle to set
	 */
	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	/**
	 * @return the keyword
	 */
	public String getKeyword() {
		return keyword;
	}

	/**
	 * @param keyword
	 *            the keyword to set
	 */
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	/**
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * @param author
	 *            the author to set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/**
	 * @return the subtype
	 */
	public int getSubtype() {
		return subtype;
	}

	/**
	 * @param subtype
	 *            the subtype to set
	 */
	public void setSubtype(int subtype) {
		this.subtype = subtype;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the creator
	 */
	public com.toncen.samepms.basic.domain.User getCreator() {
		return creator;
	}

	/**
	 * @param creator
	 *            the creator to set
	 */
	public void setCreator(com.toncen.samepms.basic.domain.User creator) {
		this.creator = creator;
	}

	/**
	 * @return the createtime
	 */
	public Date getCreatetime() {
		return createtime;
	}

	/**
	 * @param createtime
	 *            the createtime to set
	 */
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	/**
	 * @return the publisher
	 */
	public com.toncen.samepms.basic.domain.User getPublisher() {
		return publisher;
	}

	/**
	 * @param publisher
	 *            the publisher to set
	 */
	public void setPublisher(com.toncen.samepms.basic.domain.User publisher) {
		this.publisher = publisher;
	}

	/**
	 * @return the publishtime
	 */
	public Date getPublishtime() {
		return publishtime;
	}

	/**
	 * @param publishtime
	 *            the publishtime to set
	 */
	public void setPublishtime(Date publishtime) {
		this.publishtime = publishtime;
	}

	/**
	 * @return the isPublish
	 */
	public int getIsPublish() {
		return isPublish;
	}

	/**
	 * @param isPublish
	 *            the isPublish to set
	 */
	public void setIsPublish(int isPublish) {
		this.isPublish = isPublish;
	}

	/**
	 * @return the sequence
	 */
	public int getSequence() {
		return sequence;
	}

	/**
	 * @param sequence
	 *            the sequence to set
	 */
	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	/**
	 * @return the browsecount
	 */
	public int getBrowsecount() {
		return browsecount;
	}

	/**
	 * @param browsecount
	 *            the browsecount to set
	 */
	public void setBrowsecount(int browsecount) {
		this.browsecount = browsecount;
	}

	public List<ChanelInformation> getChanelInformation() {
		return chanelInformation;
	}

	public void setChanelInformation(List<ChanelInformation> chanelInformation) {
		this.chanelInformation = chanelInformation;
	}

	/**
	 * 判断是否最近3天内发布
	 * 
	 * @param publishtime
	 * @return
	 * @throws Exception
	 */
	public boolean isNew() throws Exception {
		if (this.getPublishtime() == null) {
			return false;
		}
		Configuration configuration = new ConfigurationDao().getByName("new_information_days");
		int newInformationDays = Integer.parseInt(configuration.getValue());
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String eDate = sf.format(new Date());
		String sDate = sf.format(this.getPublishtime());
		int dayDiff = DateTools.getDayDiff(sDate, eDate);
		return dayDiff <= newInformationDays;
	}

	/**
	 * 截取标题指定长度的字符串,如果超过则取长度字符加 ...
	 * 
	 * @param maintitle
	 *            要截取的字符串
	 * @param length
	 *            指定长度
	 * @return
	 */
	public String getTitle(int length) {
		if (maintitle.getBytes().length > length) {
			String index;
			int total = 0;
			for (int i = 0; i < maintitle.length(); i++) {
				index = maintitle.substring(i, i + 1);
				total += index.getBytes().length;
				if (total >= length) {
					return maintitle.substring(0, i + 1) + "...";
				}
			}
		}
		return maintitle;
	}

	/**
	 * 获取图片
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getImageSrc() {
		String regex = ".+ src=\".*/attached/image/.+/.*\\..+\" .+";// 图片正则表达式
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(getContent());
		if (matcher.find()) {
			String image = matcher.group(0);// 取到图片html
			int beginIndex = image.indexOf(" src=\"") + " src=\"".length();
			int endIndex = image.indexOf("\" ", beginIndex);
			String src = image.substring(beginIndex, endIndex);
			return src;
		}
		return null;
	}

	public String getPrimaryName() {
		// TODO Auto-generated method stub
		return "ID";
	}

	public String getTableName() {
		// TODO Auto-generated method stub
		return "NET_INFORMATION";
	}
}
